package com.lch.webdemo.config;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.filter.CharacterEncodingFilter;

import java.nio.charset.Charset;

/**
 * <一句话简述> <详情介绍>
 *
 * @author wangzhuo
 * @since wiki链接
 */
@Configuration
public class WebMvcConfig {


    @Bean
    public FilterRegistrationBean utfFilter() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(utfCharacterEncodingFilter());
        registrationBean.addUrlPatterns("/*");
        registrationBean.setName("utfFilter");
        registrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE);
        return registrationBean;
    }

    /**
     * beacon utf8过滤器
     */
    @Bean
    public CharacterEncodingFilter utfCharacterEncodingFilter() {
        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        characterEncodingFilter.setEncoding(Charset.forName("utf-8").name());
        characterEncodingFilter.setForceEncoding(true);
        characterEncodingFilter.setForceRequestEncoding(true);
        characterEncodingFilter.setForceResponseEncoding(true);
        return characterEncodingFilter;
    }

//    @Bean
//    public FilterRegistrationBean siteMeshFilter() {
//        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
//        WebSiteMeshFilter siteMeshFilter = new WebSiteMeshFilter();
//        registrationBean.setFilter(siteMeshFilter);
//        return registrationBean;
//    }
}
